#include <iostream>
#include <string>

using namespace std;

struct student{
    string id;
    int score;
};

student a[50];

void qsort(int l,int r){
    int mid = a[(l+r) /2].score;
    int i=l,j = r;
    while (i <= j){
        while (a[i].score > mid)
            i++;
        while (a[j].score < mid)
            j--;
        if (i <=j ){
            student p = a[j];
            a[j] = a[i];
            a[i] = p;
            i++;j--;
        }
    }
    if (l < j) qsort(l,j);
    if (i < r) qsort(i,r);
}


int main(){

    int n;
    cin >> n;

    for (int i = 0;i < n;i++){
        cin >> a[i].id >> a[i].score;
    }

    qsort(0,n-1);

    for (int i = 0;i <n -1;i++){
        if (a[i].id > a[i+1].id && a[i].score == a[i+1].score ){
            student p = a[i];
            a[i] = a[i+1];
            a[i+1] = p;
        }
    }


    for (int i = 0;i<n;i++){
        cout << a[i].id << " "<< a[i].score << endl;
    }

    return 0;
}